﻿
Imports System.IO
Imports std
Imports xmlctrl
Imports xhtmlctrl

Public Class linkset
    Inherits container

    Public Sub New(Optional ByVal buff As Stream = Nothing)
        MyBase.New(buff)
    End Sub

    Public Sub New(ByVal buff As Stream, ByVal links As vector(Of String) _
                   , ByVal titles As vector(Of String), ByVal images As vector(Of String))
        MyBase.New(buff)
        setdata(links, titles, images)
    End Sub

    Public Sub setdata(ByVal links As vector(Of String), ByVal titles As vector(Of String) _
                       , ByVal images As vector(Of String), Optional ByVal defaultImage As String = Nothing _
                       , Optional ByVal target As String = "_blank", Optional ByVal maxCount As Int64 = 10)
        Dim till As Int64
        If links.size() <> titles.size() _
            OrElse links.size() <> images.size() _
            OrElse titles.size() <> images.size() Then
            raiseError("links / titles / images are not paired, counts are different, use the smallest size." _
                       , errorHandle.errorType.warning)
            till = min(links.size(), min(titles.size(), images.size()))
        Else
            till = links.size()
        End If
        If maxCount > 0 Then
            till = min(till, maxCount)
        End If

        Dim i As Int64
        For i = 0 To till - 1
            If Not isemptystring(links(i)) Then
                Dim line As div = Nothing
                line = New div(output())
                line.class = "c_line"
                addchild(line)

                If isemptystring(images(i)) Then
                    images(i) = defaultImage
                End If

                If Not isemptystring(images(i)) Then
                    Dim img As img = Nothing
                    img = New img(output())
                    img.src = images(i)
                    img.height = "12px"
                    img.class = "c_img"
                    line.addchild(img)
                End If

                Dim a As a = Nothing
                a = New a(output())
                a.class = "c_a"
                If isemptystring(titles(i)) Then
                    a.text = links(i)
                Else
                    a.text = titles(i)
                End If
                a.href = links(i)
                a.target = target
                line.addchild(a)
            End If
        Next
    End Sub

End Class
